package org.study.event.test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.Log4jConfigurer;
import org.study.event.core.util.Maps;
import org.study.event.core.util.Maps.Covert;
import org.study.event.core.util.Maps.Pair;
import org.study.event.test.business.OrderBusService;
import org.study.event.test.po.Good;
import org.study.event.test.po.Orders;
import org.study.event.test.service.GoodService;
import org.study.event.test.service.OrdersService;
import org.study.event.test.vo.OrderCreateDetailVo;
import org.study.event.test.vo.OrderCreateVo;

public class AppMain {

	private static Logger logger = LoggerFactory.getLogger(AppMain.class);
	static {
		try {
			Log4jConfigurer.initLogging("classpath:log4j/log4j.properties", 5000);
		} catch (Exception e) {
			logger.error("favorite.server.App", e);
		}
	}

	private static ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
			"spring/applicationContext.xml");

	public static void main(String[] args) {
		testFindOrder();
	}

	/***
	 * 查询订单
	 */
	public final static void testFindOrder() {
		OrdersService ordersService = applicationContext.getBean(OrdersService.class);
		Orders orders = ordersService.findOrderByOrderId(1514460970698L);
		System.out.println(orders.getOrderNo());
	}

	/**
	 * 批量生成
	 */
	public final static void testBatchSaveGoodList() {
		final GoodService goodService = applicationContext.getBean(GoodService.class);
		List<Good> goodList = new ArrayList<>();
		for (int index = 0; index < 10; index++) {
			goodList.add(generateGood(index + 1));
		}
		goodService.batchSaveGood(goodList);

	}

	/***
	 * 生成订单操作
	 */
	public final static void testCreateOrder() {

		OrderCreateVo vo = new OrderCreateVo();
		List<OrderCreateDetailVo> list = new ArrayList<>();

		OrderCreateDetailVo vo1 = new OrderCreateDetailVo();
		vo1.setGoodCount(2);
		vo1.setGoodId(1514454602793L);
		list.add(vo1);

		OrderCreateDetailVo vo2 = new OrderCreateDetailVo();
		vo2.setGoodCount(3);
		vo2.setGoodId(1514454602794L);
		list.add(vo2);

		OrderCreateDetailVo vo3 = new OrderCreateDetailVo();
		vo3.setGoodCount(4);
		vo3.setGoodId(1514454602795L);
		list.add(vo3);

		vo.setDetailVos(list);
		vo.setCreateTime(new Date());
		vo.setCustomerTel("18660927735");
		vo.setCustomerName("张三");

		final OrderBusService busService = applicationContext.getBean(OrderBusService.class);
		busService.createOrder(vo);
	}

	/***
	 * 测试事件操作
	 */
	public final static void testTransactionMessage() {
		final GoodService goodService = applicationContext.getBean(GoodService.class);

		Thread t1 = new Thread(new Runnable() {
			@Override
			public void run() {
				for (int index = 0; index < 10; index++) {
					if (index % 2 == 0) {
						goodService.saveGood(AppMain.generateGood(index));
					} else {
						goodService.saveOtherGood(AppMain.generateGood(index));
					}
					try {
						Thread.sleep(300);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}, "Thread-t1");

		Thread t2 = new Thread(new Runnable() {

			@Override
			public void run() {
				for (int index = 0; index < 10; index++) {
					if (index % 2 != 0) {
						goodService.saveGood(AppMain.generateGood(index + 50));
					} else {
						goodService.saveOtherGood(AppMain.generateGood(index + 50));
					}
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {

						e.printStackTrace();
					}
				}

			}
		});

		t1.start();
		t2.start();

	}

	public final static Good generateGood(int index) {
		Good good = new Good();
		good.setGoodCount(index + 1);
		good.setGoodId(System.currentTimeMillis() + (index + 1));
		good.setGoodName("商品" + (index + 1));
		good.setGoodSaleState(1);
		good.setGoodState(1);
		good.setMarginCount(index + 1);
		good.setPrice(10000.0);
		good.setSaleCount(0);

		List<Good> list = new ArrayList<>();
		list.add(good);

	   Map<Long, String> map=Maps.getMapFromCollection(list, new Covert<Good, Long, String>() {
			@Override
			public Pair<Long, String> covertPair(Good m) {
				return Maps.buildPair(m.getGoodId(), m.getGoodName());
			}
		});

		return good;

	}

}
